package com.scpii.universal.pns.action;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.doone.comm.paginaction.Pagination;
import com.scpii.universal.action.BaseAction;
import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.pns.dto.EcOrderDto;
import com.scpii.universal.pns.dto.EcOrderSearchDto;
import com.scpii.universal.pns.service.EcOrdersService;

@Controller
@Scope("prototype")
public class PnsOrderAction extends BaseAction {
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 4858773510657698933L;
	@Resource
	private EcOrdersService ecOrdersService;
	private Pagination page = new Pagination();
	private Pagination pageDetail = new Pagination();
	private List<EcOrderDto> orderDto;
	private EcOrderSearchDto condition;
	private static Map<Short,String> orderStatusList;
	private static Map<Short,String> shipStatusList;
	private static Map<Short,String> payStatusList;
	private static Map<Short,String> productStatusList;
	
	static {
		orderStatusList = new HashMap<Short,String>();
		orderStatusList.put((short) -1, "订单状态");
//		orderStatusList.put((short) 0, "未确认");
		orderStatusList.put((short) 1, "已确认");
//		orderStatusList.put((short) 2, "已取消");
//		orderStatusList.put((short) 3, "无效");
//		orderStatusList.put((short) 4, "退货");
		orderStatusList.put((short) 5, "交易成功");
		payStatusList = new HashMap<Short,String>();
		payStatusList.put((short) -1, "付款状态");
		payStatusList.put((short) 0, "未付款");
//		payStatusList.put((short) 1, "付款中");
		payStatusList.put((short) 2, "已付款");
		shipStatusList = new HashMap<Short,String>();
		shipStatusList.put((short) -1, "发货状态");
		shipStatusList.put((short) 0, "未发货");
//		shipStatusList.put((short) 1, "配货中");
		shipStatusList.put((short) 2, "已发货");
//		shipStatusList.put((short) 3, "已收货");
		productStatusList = new HashMap<Short,String>();
		productStatusList.put((short) -1, "商品类型");
		productStatusList.put((short) 1, "实体商品");
		productStatusList.put((short) 0, "虚拟商品");
	}

	public String getOrderInfo() {
		if (condition == null) {
			condition = new EcOrderSearchDto();
			condition.setOrderStatus((short)-1);
			condition.setPayStatus((short)-1);
			condition.setShipStatus((short)-1);
			condition.setProductIsReal((short)-1);
			condition.setKeyWord("订单编号/收货人/手机号/物流单号");
		}
		if (getSelectPerPage("page") == -1) {
			page.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			page.setPageRecord(getSelectPerPage("page"));
		}
		// 判断是否是第一次查询
		if (getQueryFlag("page") == -1) {
			page.setTotalRecord(ecOrdersService.getAllCount(
					condition,getUserInfo().getAppId()+GenericConstants.APP_ID_PRE));
		} else {
			page.setTotalRecord(getQueryFlag("page"));
		}
		setPageParameters(page, "page");
		orderDto = ecOrdersService.getAll(condition,getUserInfo().getAppId()+GenericConstants.APP_ID_PRE, page.getCurrentPage(), page.getPageRecord());
		return SUCCESS;
	}
	
	public String doOrderSend() throws IOException {
		Long id = Long.parseLong(request.getParameter("id"));
		String shipType = request.getParameter("shipType");
		shipType = URLDecoder.decode(URLDecoder.decode(shipType, "UTF-8"), "UTF-8");
		String shipSn = request.getParameter("shipSn");
		shipSn = URLDecoder.decode(URLDecoder.decode(shipSn, "UTF-8"), "UTF-8");
		ecOrdersService.updateShipInfo(id, shipType, shipSn);
		return null;
	}

	public Pagination getPage() {
		return page;
	}

	public EcOrderSearchDto getCondition() {
		return condition;
	}

	public void setCondition(EcOrderSearchDto condition) {
		this.condition = condition;
	}

	public Pagination getPageDetail() {
		return pageDetail;
	}

	public void setPageDetail(Pagination pageDetail) {
		this.pageDetail = pageDetail;
	}

	public List<EcOrderDto> getOrderDto() {
		return orderDto;
	}

	public void setOrderDto(List<EcOrderDto> orderDto) {
		this.orderDto = orderDto;
	}

	public  Map<Short, String> getOrderStatusList() {
		return orderStatusList;
	}

	public  void setOrderStatusList(Map<Short, String> orderStatusList) {
		PnsOrderAction.orderStatusList = orderStatusList;
	}

	public  Map<Short, String> getShipStatusList() {
		return shipStatusList;
	}

	public  void setShipStatusList(Map<Short, String> shipStatusList) {
		PnsOrderAction.shipStatusList = shipStatusList;
	}

	public  Map<Short, String> getPayStatusList() {
		return payStatusList;
	}

	public  void setPayStatusList(Map<Short, String> payStatusList) {
		PnsOrderAction.payStatusList = payStatusList;
	}

	public  Map<Short, String> getProductStatusList() {
		return productStatusList;
	}

	public  void setProductStatusList(Map<Short, String> productStatusList) {
		PnsOrderAction.productStatusList = productStatusList;
	}

}
